package com.star95.springbooti18n.controller;

import com.star95.springbooti18n.dto.CustomerUserDto;
import com.star95.springbooti18n.dto.Result;
import com.star95.springbooti18n.exception.ExceptionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import java.util.UUID;

import static com.star95.springbooti18n.common.constants.MessagesEnum.USERNAME_PASSWORD_INCORRECT;

/**
 * 客户端接口
 */
@Slf4j
@RestController
@RequestMapping("/customer")
@Api(tags = "客户端接口")
@Validated
public class CustomerController {
    @PostMapping("/login")
    @ApiOperation("客户端登录")
    public Result<String> login(@Validated @RequestBody CustomerUserDto customerUserDto) {
        log.info("客户端登录请求参数，customerUserDto:{}", customerUserDto);
        if (!"admin".equals(customerUserDto.getCustomerAccount())) {
            throw ExceptionUtil.exception(USERNAME_PASSWORD_INCORRECT);
        }
        log.info("用户账号:{},登录成功！", customerUserDto.getCustomerAccount());
        String token = UUID.randomUUID().toString();
        return Result.ok(token);
    }

    @PostMapping("/getCustomerInfo")
    @ApiOperation("获取客户信息")
    public Result<CustomerUserDto> getCustomerInfo(@RequestParam("id") @Max(value = 9999, message = "{id_max_error}") int id) {
        log.info("客户端获取客户信息请求参数，id:{}", id);
        CustomerUserDto customerUserDto = new CustomerUserDto();
        customerUserDto.setCustomerAccount("1111");
        return Result.ok(customerUserDto);
    }
}
